package com.beself.controller;

import com.beself.entity.TModule;
import com.beself.entity.TRoute;
import com.beself.entity.TTask;
import com.beself.enums.SuccessPersentEnum;
import com.beself.service.JourneyService;
import com.beself.until.ModuleStateUtils;
import com.beself.until.RestJson;
import com.beself.until.SuccessPersentUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ProjectName: beself
 * @Package: com.beself.controller
 * @ClassName: JourneyController
 * @Description: java类作用描述
 * @Author: 樊晋源
 * @CreateDate: 2019/10/16 9:46
 * @Version: 1.0
 */
@Controller
public class JourneyController {
    @Resource
    private JourneyService journeyService;

    @RequestMapping("/api/journey/get_all_travel")
    @ResponseBody
    public RestJson getAllTravel(int uId){
        //查询所有旅程线路
        List<TRoute> routeList =journeyService.findroutes();
        //遍历线路，查询模块
        for (TRoute route : routeList) {
        //查询该用户下面每个线路的各完成多少任务，
        //查询t_usertask表，参数ut_uid，ut_route，count(*),查询条件ut_status=1
            int jd = journeyService.findachievement(uId,route.getrId());
            //将完成度转化成百分比
            int bfb = SuccessPersentUtils.change(jd);
            //rA属性表示完成度1/15
            route.setrA(jd);
            //rB属性表示百分比6%
            route.setrB(bfb);

            //查询每个线路所有模块
            List<TModule> moduleList =journeyService.findmodules(route.getrId());
            for (TModule module : moduleList) {
                //查询每个模块的状态
                String state = ModuleStateUtils.state(jd, module.getmId());
                //设置当前线程下每个模块的状态
                module.setmB(state);

                //查询每个模块下所有的任务
                List<TTask> taskList =journeyService.findtasks(module.getmId());
                //将任务存在模块里
                module.settTaskList(taskList);

            }
            //将模块存在旅程路线里
            route.settModuleList(moduleList);

        }
        //将旅程路线存在每个用户里
        if (routeList.size()>0){
            return new RestJson(true,"成功",routeList);
        }
        return new RestJson(false,"失败",null);
    }


    //打卡
    @RequestMapping("/api/clockIn")
    @ResponseBody
    public RestJson clockIn(int uId,int routeId,int taskId){

        int a =journeyService.clock(uId,routeId,taskId);

        if(a>0){
            return new RestJson(true, "成功", null);
        }
        return new RestJson(false,"失败",null);
    }



}
